package org.example.struct.array3;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author liyishan
 * @date 2024/4/8 09:35
 * @apiNote
 */

public class Test02009MinOperations {

    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,5,6};
        System.out.println(minOperations(nums));
    }

    public static int minOperations(int[] nums) {
        int n = nums.length;
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        List<Integer> sortedUniqueNums = set.stream().sorted().collect(Collectors.toList());
        int res = n;
        int j = 0;
        for (int i = 0; i < sortedUniqueNums.size(); i++) {
            int left = sortedUniqueNums.get(i);
            int right = left +n -1;
            while (j < sortedUniqueNums.size() && sortedUniqueNums.get(j) <= right) {
                res = Math.min(res,n-(j-i+1));
                j++;
            }
        }
        return res;
    }
}
